package com.suanfa.string;

import com.alibaba.fastjson.JSON;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 字母异位词，相同字母不同顺序子串的开始下标
 */
public class StringIndex {

    public static void main(String[] args) {
        String s = "abab", p = "ab";
        System.out.println(JSON.toJSONString(getIndexArray(s,p)));
    }


    public static List<Integer> getIndexArray(String s, String p){
        List<Integer> list = new ArrayList<>();
        if(s.length() < p.length()){
            return list;
        }

        int[] pb = new int[26];

        for(int i =0; i<p.length(); i++){
            pb[p.charAt(i)-'a']++;
        }
        int pLength = p.length();
        char[] c = s.toCharArray();
        for(int i=0; i< c.length-pLength+1; i++){
            char ch = c[i];
            //符合条件的
            if(p.indexOf(ch) > -1){
                boolean contain = true;
                int[] arr = new int[26];
                for(int j=0; j<pLength && i+j<c.length; j++){
                   arr[s.charAt(i+j) - 'a']++;
                }
                if (Arrays.equals(pb, arr)){
                    list.add(i);
                }
            }
        }
        return list;

    }

    public static boolean isSame(int[] a1, int[] a2){
        for(int i=0; i< a1.length; i++){
            if(a1[i] != a2[i]){
                return  false;
            }
        }
        return true;
    }
}
